package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.ConfigMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.dataHelper.utils.ExcelData;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.utils.date.DateHelper;

import java.io.File;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class AcMall extends BaseRepair {

    public AcMall(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }
    public void go(int activityId){
        getLogger().error("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        String dirPath = System.getProperty("user.dir") + File.separator;
        ExcelData sheet0 = new ExcelData(dirPath + "inExcel" + File.separator + "pass.xlsx", "sheet0");
        System.err.println(sheet0.getExcelData());
    }
    //触发礼包一条龙三个日志
    public void go1(int activityId){
        List<Map<String, String>> listMap = new ArrayList<>();
        BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
        ActivityInfo activityInfo = ActivityMgr.getActivityInfo(activityId);
        if(activityInfo == null){
            System.err.println("没有找到活动");return;
        }
        if(!activityInfo.getServerIdList().contains(getGameServer().getServerInfo().getServerId())){
            System.err.println(activityId + "不是" + getGameServer().getServerInfo().getServerId()+"的活动");return;
        }
        String dateStart = DateHelper.getCurrentDateTimeString(activityInfo.getBeginShowTime()*1000);
        String dateEnd = DateHelper.getCurrentDateTimeString(activityInfo.getEndShowTime()*1000);
        dateStart = "2021-1-17 10:00:00";
        dateEnd = "2021-1-18 22:00:00";
        listMap = getLog1(activityId,dateStart,dateEnd);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLog1(int activityId,String dateStart,String dateEnd) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select *,count(1) as num " +
                " from t_l_buy " +
                " where `time` > ? AND `time` < ? AND shopType = 12 GROUP BY mallId,userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setString(i++,dateStart);
            call.setString(i++,dateEnd);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("activityId",String.valueOf(activityId));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("mallId",checkNullToEmpty(rs.getString("mallId")));
//                temp.put("time",checkNullToEmpty(rs.getString("time")));
                temp.put("price",checkNullToEmpty(rs.getString("price")));
//                temp.put("shopType",checkNullToEmpty(rs.getString("shopType")));
//                temp.put("goods",checkNullToEmpty(rs.getString("goods")));
                temp.put("num",checkNullToEmpty(rs.getString("num")));
                temp.put("mallName", ConfigMgr.getMallName(Integer.valueOf(temp.get("mallId"))));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }
}
